package Button
{
	import Const.ButtonIDs;
	
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.events.MouseEvent;
	
	public class UglyButton extends Sprite
	{
		private var _text:TextField = new TextField();
		private var _textFormat : TextFormat = new TextFormat();		
		
		private var _id:int = ButtonIDs.BUTTONID_NONE;
		
		private var _buttonBackground : Sprite = new Sprite();
		private var _buttonEffect : Sprite = new Sprite();
		
		public function UglyButton(text:String, id:int)
		{
			super();
			
			_id = id;
			
			//Set Button Properties
			this.mouseChildren = false;
			this.buttonMode = true;
			this.name = String(_id);			
			
			//Set Button Text
			if(text != "")
			{						
				//Setting some native properties			
				_textFormat.size = 17;
				_textFormat.bold;
				_textFormat.align = TextFormatAlign.CENTER;
				_text.defaultTextFormat = _textFormat;
				
				//_text.autoSize = "center";
				_text.selectable = false; _text.height = 20;
				
				//Assigning the text
				_text.text = text;			
				
				_buttonBackground.addChild(_text);
			}
			//Set Button Background
			with(_buttonBackground.graphics)
			{			
				beginFill(0x55FF55, 1);
				drawRect(0, 0, 100, 20);
				endFill();
			}
			
			with(_buttonEffect.graphics)
			{
				beginFill(0x000000, 1);
				drawRect(2, 2, 100, 20);
				endFill();
			}
			addChild(_buttonEffect);
			addChild(_buttonBackground);
			
			addListeners();
		}
		
		public function setPos(posX:Number, posY:Number) : void
		{
			this.x = posX;
			this.y = posY;
		}
		
		public function getPos() : Point
		{
			return new Point(this.x, this.y);
		}
		
		public function getId() : int
		{
			return _id;
		}
		
		private function addListeners() : void
		{
			this.addEventListener(MouseEvent.ROLL_OUT, mouseEvents, false, 0, true);
			this.addEventListener(MouseEvent.ROLL_OVER, mouseEvents, false, 0, true);
			this.addEventListener(MouseEvent.MOUSE_DOWN, mouseEvents, false, 0, true);
			this.addEventListener(MouseEvent.MOUSE_UP, mouseEvents, false, 0, true);
		}
		
		private function mouseEvents(event:MouseEvent) : void
		{
			switch(event.type)
			{
				case MouseEvent.ROLL_OUT:
					with(_buttonBackground.graphics)
					{
						beginFill(0x55FF55, 1);
						drawRect(0, 0, 100, 20);
						endFill();
					}
					this._buttonEffect.visible = true;
					break;
				case MouseEvent.ROLL_OVER:
					with(_buttonBackground.graphics)
					{
						beginFill(0x088A08, 1);
						drawRect(0, 0, 100, 20);
						endFill();
					}					
					break;
				case MouseEvent.MOUSE_UP:
					this._buttonEffect.visible = true;
					break;
				case MouseEvent.MOUSE_DOWN:
					this._buttonEffect.visible = false;
					break;
			}		
		}
	}
}